import { defineStore } from "pinia";
// import { positionType, UserType } from "@/types";
// import { $t } from "@/locales";
import { useStorage } from "@vueuse/core";

function convertToUpperCase(str:string) {
    let result = '';
    for (let i = 0; i < str.length; i++) {
        result += str[i].toUpperCase();
    }
    return result;
}

/**
 * 默认应用名称
 */
const appName = window.appConfig.name||"admin"
const upperName = convertToUpperCase(appName)

declare type appTypes = {
    positions: any
    token: string,
    isLogin: any,
    user: any,
    setting: any
};

export const useAppStore = defineStore("app", {
    state: (): appTypes => {
        return {
            positions: [],
            // @ts-ignore
            token: useStorage<string>(`${upperName}_PC_TOKEN`, ""),
            isLogin: useStorage<boolean>(`${upperName}_PC_IS_LOGIN`, false),
            user: useStorage<any>(`${upperName}_PC_USER`, {
                id: "",
                name: ""
            }),
            setting: useStorage<any>(`${upperName}_PC_SETTING`, {
                theme: "smb",
            }),
        };
    },
    actions: {
        getPosition(name: string) {
            let index = this.positions.findIndex((item: any) => item.name === name);
            if (index !== -1) {
                return this.positions[index];
            } else {
                return {
                    left: 0,
                    top: 0,
                    name,
                };
            }
        },
        setPosition(object: any) {
            let index = this.positions.findIndex(
                (item: any) => item.name === object.name,
            );
            if (index === -1) {
                this.positions.push(object);
            } else {
                this.positions[index] = object;
            }
        },
        setToken(token:string){
            this.token = token;
        },
        getToken(): string {
            return this.token
        },
        setIsLogin(bool:boolean){
            this.isLogin = bool;
            if (this.isLogin){
                // this.loginDialog = false;
            }
        },
        getIsLogin(): boolean{
            return this.isLogin
        },
        setUser(user: any){
            this.user = user;
        },
        getUser(): any {
            return this.user
        },
        setSetting(setting: any){
            this.setting = setting;
        },
        getSetting(): any {
            return this.setting
        }
    },
});
